package com.jinlejia.bbs.adapter;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jinlejia.bbs.R;
import com.jinlejia.bbs.bean.InviteBean;
import com.jinlejia.bbs.bean.base.TopicBean;
import com.jinlejia.bbs.databinding.BbsAdapterInviteItemBinding;
import com.jinlejia.bbs.logic.IgnoreInviterecordLogic;
import com.jinlejia.bbs.ui.reply.BBSReplyDetailActivity;

import java.util.ArrayList;

/**
 * @author 王进 on 2019/3/15.
 */

public class InviteAdapter extends BaseLoadAdapter<ArrayList<InviteBean>> {
    private final int TYPE_DATA = 1;
    public InviteAdapter(Context context) {
        super(context);
    }

    @Override
    protected RecyclerView.ViewHolder onCreateViewHolder(LayoutInflater inflater, ViewGroup parent, int viewType) {
        switch (viewType){
            case TYPE_DATA:
                return new ItemViewHolder(inflater.inflate(R.layout.bbs_adapter_invite_item, parent, false));
            default:
                return null;
        }
    }

    @Override
    protected int getItemViewTypeZ(int position) {
        return TYPE_DATA;
    }
    private IgnoreInviterecordLogic logic;
    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof ItemViewHolder){
            if (logic==null)
                logic = new IgnoreInviterecordLogic(mContext);
            BbsAdapterInviteItemBinding binding = DataBindingUtil.getBinding(holder.itemView);
            final InviteBean bean = mData.get(position);
            binding.setBean(bean);
            View.OnClickListener toTopicListener =new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    intent.setClass(mContext, BBSReplyDetailActivity.class);
                    TopicBean topic = new TopicBean();
                    topic.setTopicCode(bean.getTopic().getTopicCode());
                    intent.putExtra("isInvite",true);
                    intent.putExtra("topic",topic);
                    mContext.startActivity(intent);
                }
            };

            binding.topicClickView.setOnClickListener(toTopicListener);
            binding.titleTextView.setOnClickListener(toTopicListener);
            if (bean.isIgnoreState()){
                binding.ignoreTextView.setText("已忽略");
                binding.ignoreTextView.setEnabled(false);
            }else {
                binding.ignoreTextView.setText("忽略");
            }
            binding.ignoreTextView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    logic.execute(bean.getId(), new IgnoreInviterecordLogic.LogicCallBack() {
                        @Override
                        public void onResult(boolean result) {
                            if (ignoreListener!=null){
                                ignoreListener.onIgnoreSuccess();
                            }else {
                                notifyItemChanged(position);
                            }
                        }
                    });
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        if (mData==null || mData.size()==0){
            return 1;
        }
        return mData.size();
    }

    public interface IgnoreListener{
        void onIgnoreSuccess();
    }

    private IgnoreListener ignoreListener;

    public void setIgnoreListener(IgnoreListener ignoreListener) {
        this.ignoreListener = ignoreListener;
    }

    private class ItemViewHolder extends RecyclerView.ViewHolder {

        ItemViewHolder(View itemView) {
            super(itemView);
            DataBindingUtil.bind(itemView);
        }
    }
}
